package leecode

func longestMonotonicSubarray(nums []int) int {
	// 边界情况处理
	if len(nums) <= 1 {
		return len(nums)
	}

	// 初始化变量
	maxLength := 1
	increasingLength := 1
	decreasingLength := 1

	// 遍历数组
	for i := 1; i < len(nums); i++ {
		// 当前元素大于前一个元素，递增长度加一，递减长度重置为1
		if nums[i] > nums[i-1] {
			increasingLength++
			decreasingLength = 1
			// 当前元素小于前一个元素，递减长度加一，递增长度重置为1
		} else if nums[i] < nums[i-1] {
			decreasingLength++
			increasingLength = 1
			// 如果相等，重置递增和递减长度
		} else {
			increasingLength = 1
			decreasingLength = 1
		}

		// 更新最大长度
		maxLength = max(maxLength, max(increasingLength, decreasingLength))
	}

	return maxLength
}
